package aaa.schedule.parameters;

import aaa.main.R;
import aaa.screen.CreateScheduleScreen;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

/**
 *
 * @author Seb
 */
public class ScheduleResource extends ParameterType {
	
	private static int currentId = 0 ;
	
	public static final String FIELD_ID = "ressources" ;

	private String ressource ;
	private LayoutInflater inflater ;
	protected CreateScheduleScreen cs ;
	protected int id ;

	/**
	 * @param ressource
	 * @param inflater
	 */
	public ScheduleResource(LayoutInflater inflater, String ressource, CreateScheduleScreen cs) {
		super(FIELD_ID);
		this.ressource = ressource;
		this.inflater = inflater;
		this.cs = cs ;
		this.id = ScheduleResource.currentId++ ;
	}

	public Object getValue(View view) {
		EditText ress = (EditText) view.findViewById(R.id.refSchedule) ;
		return ress.getText().toString() ;
	}

	public View getView(View convertView, ViewGroup parent) {
		convertView = inflater.inflate(R.layout.type_schedule_reference, null) ;
		ImageView minusIcon = (ImageView) convertView.findViewById(R.id.remove_ressource) ;
		minusIcon.setOnClickListener(new RemoveRessourceListener(this.cs, convertView,  this.id)) ;
		EditText ress = (EditText) convertView.findViewById(R.id.refSchedule) ;
		ress.setText(this.ressource) ;
		Button choice = (Button) convertView.findViewById(R.id.choiceRef) ;
		choice.setEnabled(false) ;
		return convertView ;
	}
	
	public int getRessourceId() {
		return this.id;
	}
	
	class RemoveRessourceListener implements OnClickListener {	
		
		protected CreateScheduleScreen cs ;
		protected View parent ;
		protected int id ;
		
		/**
		 * @param cs
		 */
		public RemoveRessourceListener(CreateScheduleScreen cs, View parent, int id) {
			super();
			this.cs = cs;
			this.parent = parent ;
			this.id = id ;
		}


		public void onClick(View v) {
			this.cs.removeResource(this.parent, this.id) ;
		}
	}

}
